package com.gougou.pic.service;

import com.gougou.pic.entity.PicUploadResult;
import com.gougou.pic.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Service
@PropertySource("classpath:pic.properties")
public class PicUploadService {

    @Value("${upload.ROOT_PATH}")
    private String ROOT_PATH;

    @Value("${upload.ROOT_URL}")
    public  String ROOT_URL;


    /*****************************
     *
     * 传入文件 和 保存地址
     *
     * @param uploadFile
     * @param path
     * @return
     * ******************************
     */
    public PicUploadResult fileUpload(MultipartFile uploadFile, String path){
        String filename=uploadFile.getOriginalFilename();

        //写入操作
        FileUtils.upload(uploadFile, this.ROOT_PATH+path,filename);

        // 校验图片是否合法(并写入返回值)---------------------------------------

        File newFile = new File(this.ROOT_PATH+path+"/"+filename);

        return FileUtils.parsePicResult(newFile,ROOT_URL,path,filename);
    }




}
